package com.come4loves.game.module.skill;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;

import com.come4loves.core.utils.RandomUtils;
import com.come4loves.game.dataconfig.model.GeneralCardConfig;
import com.come4loves.game.dataconfig.model.UnitCardConfig;
import com.come4loves.game.dataconfig.service.GeneralCardService;
import com.come4loves.game.dataconfig.service.UnitCardService;
import com.come4loves.game.module.card.constant.CardType;
import com.come4loves.game.module.fight.model.FightCard;

public abstract class AbstractTargetCards implements SkillTargetCards {
	private static Map<Integer,AbstractTargetCards> targetHandler = new HashMap<>();
	@PostConstruct
	private void init(){
		targetHandler.put(this.id(), this);
	}
	
	public static AbstractTargetCards get(int id) {
		return targetHandler.get(id);
	}
	@Override
	public Collection<FightCard> filterRace(Collection<FightCard> cards, int race) {
		List<FightCard> result = new ArrayList<FightCard>();
		for (FightCard fightCard : cards) {
			if (fightCard.getCardType().equals(CardType.UNIT)){
				UnitCardConfig cfg = UnitCardService.get(fightCard.getCfgId());
				if (cfg != null && cfg.getRace() == race) {
					result.add(fightCard);
				}
			}
			if (fightCard.getCardType().equals(CardType.GENERAL)){
				GeneralCardConfig cfg = GeneralCardService.get(fightCard.getCfgId());
				if (cfg != null && cfg.getRace() == race) {
					result.add(fightCard);
				}
			}
		}
		return result;
	}
	
	@Override
	public Collection<FightCard> filterRandom(Collection<FightCard> cards, int num) {
		return RandomUtils.randomValue(cards, num);
	}
}
